package org.liveboardgames.agricola.business.updater;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.liveboardgames.agricola.domain.Change;
import org.liveboardgames.agricola.domain.card.Card;
import org.liveboardgames.agricola.domain.common.Peep;
import org.liveboardgames.agricola.model.enums.ChangesEnum;

public class CardUpdater extends AbstractUpdater implements IResourceUpdater {

  private Card card;

  public CardUpdater(Card card, List<Change> changes) {
    super(changes);
    this.card = card;
  }

  public void removePeeps() {
    card.setPeeps(null);
    addChange();
  }

  public void addResource(String resourceType, Integer outputQuantity) {
    Map<String, Integer> resources = card.getResources();
    if (resources == null) {
      resources = new HashMap<String, Integer>();
      card.setResources(resources);
    }
    if (resources.get(resourceType) != null) {
      outputQuantity += resources.get(resourceType);
    }
    resources.put(resourceType, new Integer(outputQuantity));
    addChange();
  }

  public void addResources(Map<String, Integer> resources) {
    if (resources != null) {
      for (String type : resources.keySet()) {
        addResource(type, resources.get(type));
      }
    }
  }

  public void addPeep(Peep peep) {
    card.addPeep(peep);
    addChange();
  }

  private void addChange() {
    Change change = new Change(ChangesEnum.CARD, card);
    addChange(change);
  }

  public void resetResources() {
    card.setResources(null);
    addChange();
  }

}
